/**
 * All usefull functions to manage webpage
 * @author Łukasz Torba
*/
var SystemModule = '';

var System = {
    
    //this.SystemModule = '';
    
    /**
     * Set currently working module
     */
    setModule: function(module)
    {
       SystemModule = module;
    },
    
    /**
     * Redirect to subpage
     */
    redirect: function(subpage,param)
    {
        // build address
        // TODO: Add event
        Controller = this.getController();
        Module = this.getModule();
        Action = this.getAction();
        
        if(!(Module && Controller && subpage))
        {
            System.WindowTooltip('error','Nie można utwożyć przekierowania');
            return false;
        }
        
        var uri = '/'+Module+'/'+Controller+'/'+subpage;
        
        if(param)
        {
            uri +='/'+param.split(',');
        }
        
        document.location = uri;
    },
    
    /**
     * Abslute redirect
     */
    absRedirect: function(url)
    {
        Module = this.getModule();
        if(!Module)
        {
            System.WindowTooltip('error','Nie można utwożyć przekierowania');
            return false;
        }
        
        document.location = '/'+Module+'/'+url;
    },
    
    /**
     * Get currently working controller
     */
    getController : function()
    {
        var Controller = null;
        $.ajax({
            type: "POST",
            url: System.buildEvent('GetCurrentModule'),
            dataType: "json",
            async: false,
            success: function(data){                   
                Controller = data.controller;
            },error:function(){
                Console.log("Error in getting controller address");
            }
        });
        return Controller;
    },
    
    /**
     * Get currently loaded action
     */
    getAction : function()
    {
        var Action = null;
        $.ajax({
            type: "POST",
            url: System.buildEvent('GetCurrentModule'),
            dataType: "json",
            async: false,
            success: function(data){                                   
                Action = data.action;
            },error:function(){
                Console.log("Error in getting action address");
            }
        });
        return Action;
    },
    
    /**
     * Get currently loaded module
     */
    getModule: function()
    {
        var Module = null;
        $.ajax({
            type: "POST",
            url: System.buildEvent('GetCurrentModule'),
            dataType: "json",
            async: false,
            success: function(data){                                   
                Module = data.module;
            },error:function(){
                Console.log("Error in getting action address");
            }
        });
        return Module;
    },
    
    /**
     * Builds ajax event address
    */
    buildEvent: function(event_name)
    {        
        return '/event/'+SystemModule+'/'+event_name;
    },
    
    /**
     * Create window from hidden div
     */
    DivWindow: function(param)
    {   
        param = $.extend({},{
            'success': null,
            'cancel': null,
            'id': '',
        },param);
        

        $(window).resize(function(){        
            var div1 = $('div[class="content-window"]');
            div1.each(function(){
                x = (window.innerWidth - div1.width()) / 2;
                y = (window.innerHeight - div1.height()) /2;        
                if(y<0)
                {
                    y=10;
                    h = window.innerHeight-40;
                    w = 520;
                    div1.css({'height':h,'width':w,'overflow':'auto'});
                }
                div1.css({'left':x,'top':y});                       
           });
        }).resize();
        
        var div = $('div[id="'+param.id+'"]');
              
        $("body").append('<div class="content-background"></div>');
      
        
        div.find('#evSuccess').on("click",function(){
            $('#evSuccess').prop('disabled',true);
             $('#evCancel').prop('disabled',true);
            if(typeof(param.success) == 'function') 
            {
                if(param.success($(this)))
                {                    
                    div.fadeOut(1000, function(){
                    // $(this).hide();                                
                            $('.content-background').remove();                    
                    });                     
                }                
            }else{                  
                $('.content-background').remove();                     
            }
            $('#evSuccess').prop('disabled',false);
            $('#evCancel').prop('disabled',false);
        });   
        
        div.find('#evCancel').on("click",function(){
             $('#evSuccess').prop('disabled',true);
             $('#evCancel').prop('disabled',true);
            div.fadeOut(1000,function(){
                // $(this).hide();
                $('.content-background').remove();
                if(typeof(param.cancel) == 'function')
                {
                    param.cancel($(this));
                }
             $('#evSuccess').prop('disabled',false);
             $('#evCancel').prop('disabled',false);                
            }); 
        });              
        
        div.show();
    },
    
    /**
     * Display progress window.
     * Progress window must be closed otherwise callback
     * function will be never called
     * @param string message - Message to display
     * @param callback - function called after action           
     */
    ProgressWindow: function(message,callback)
    {
        $('.progress').remove();
        html = "<div class=\"tooltip progress window\" id=\"progress\"><p>"+message+"</p></div>";
        $('body').append(html);
        x = (window.innerWidth - $('.progress').width()) / 2;
        $('.progress').css({'left':x});
        $('.progress').fadeIn('slow');
                                        
        $('.progress').click("focus",function(){
            $(this).delay(3000).slideUp('slow',function(){
                $(this).remove()
                
                if(typeof(callback) === 'function')
                {
                    callback();
                }
            });            
        });
    },
    
    /**
     * Closing progress window 
     */
    CloseProgressWindow: function()
    {
        // TODO: Move window.css to public 
        $('.progress').click();
    },
    
    
    /**
     * Centerized window tooltip from adding new dom element     
    **/
    WindowTooltip: function(type, message, callback)
    {
        $('.tooltip').remove();
        html = "<div class=\"tooltip window\" id=\""+type+"\"></div>";
        $('body').append(html);
        this.CenterTooltip(message,'.tooltip',callback);
    },
    
    /**
     * Displays centered message tooltip on window surface
     * @param message - message content
     * @param element - identifier one of the dom tags to put message
    */
    CenterTooltip: function(message, element, callback)
    {
        if(element)
        {
            x = (window.innerWidth - $(element).width()) / 2;
            $(element).css({'left':x});
            
            this.tooltip(message, element, callback) 
        }  
    },
    
    /**
     * Displays message tooltip
     * @param message - message content
     * @param element - identifier one of the dom tags to put message
    */
    tooltip: function(message, element, callback)
    {
        if(element)
        {                        
            $(element)[0].innerHTML="<p>"+message+"</p>";
            $(element).fadeIn('slow');            
           /* $(element).delay(3000).slideUp('slow',function(){
                $(this).hide()
                
                if(typeof(callback) === 'function')
                {
                    callback();
                }
            });*/
        }
    },     
    
    /**
     * Return actualy called action
     * @return null or string
    
    getAction: function()
    {
        var action = null;
        $.ajax({
            url: System.buildEvent('Get_Action'),
            type: "post",
            dataType: 'json',
            async: false,
            success: function(data)
            {
                action = data.action;
            },
            error: function()
            {
                Console.log("Error in getting action address");
            }
        })
        
        return action;
    }*/  
        
};